local sk__yanjiao = fk.CreateSkill {

  name = "sk__yanjiao",

  tags = {},

}



sk__yanjiao:addEffect("active", {
  name = "sk__yanjiao",
  anim_type = "support",
  min_card_num = 1,
  target_num = 1,
  prompt = function(self)
    return "#sk__yanjiao" .. self.interaction.data
  end,
  interaction = function(self, player)
    local choices = {}
    for i = 1, 5, 1 do
      if not table.contains(player:getTableMark("sk__yanjiao-phase"), i) then
        table.insert(choices, tostring(i))
      end
    end
    return UI.ComboBox { choices = choices }
  end,
  card_filter = function(self, player, to_select, selected)
    if #selected < tonumber(self.interaction.data) and table.contains(player:getCardIds("h"), to_select) then
      if self.interaction.data == "1" then
        return table.every(player:getCardIds("h"), function(id)
          return Fk:getCardById(to_select).number >= Fk:getCardById(id).number
        end)
      elseif self.interaction.data == "2" then
        if #selected == 0 then
          return true
        else
          return Fk:getCardById(to_select).number == Fk:getCardById(selected[1]).number
        end
      elseif self.interaction.data == "3" then
        if #selected == 0 then
          return Fk:getCardById(to_select).suit ~= Card.NoSuit
        else
          return Fk:getCardById(to_select).suit == Fk:getCardById(selected[1]).suit
        end
      elseif self.interaction.data == "4" then
        if #selected == 0 then
          return true
        else
          return Fk:getCardById(to_select).number ~= Fk:getCardById(selected[1]).number
        end
      elseif self.interaction.data == "5" then
        if #selected == 0 then
          return Fk:getCardById(to_select).suit ~= Card.NoSuit
        else
          return Fk:getCardById(to_select).number ~= Fk:getCardById(selected[1]).number and
              Fk:getCardById(to_select).suit == Fk:getCardById(selected[1]).suit
        end
      end
    end
  end,
  target_filter = function(self, player, to_select, selected, selected_cards)
    return #selected == 0 and to_select ~= player
  end,
  feasible = function(self, player, selected, selected_cards)
    if #selected == 1 and #selected_cards == tonumber(self.interaction.data) then
      if tonumber(self.interaction.data) < 4 then
        return true
      else
        local nums = table.map(selected_cards, function(id)
          return Fk:getCardById(id).number
        end)
        table.sort(nums, function(a, b)
          return a < b
        end)
        return nums[#nums] - nums[1] + 1 == tonumber(self.interaction.data)
      end
    end
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local n = tonumber(self.interaction.data)
    room:addTableMark(player, "sk__yanjiao-phase", n)
    room:moveCardTo(effect.cards, Card.PlayerHand, target, fk.ReasonGive, sk__yanjiao.name, nil, false, player)
    if not player.dead then
      player:drawCards(n, sk__yanjiao.name)
    end
    if not player.dead and not target.dead and player:getMark("@@sk__xingshen") > 0 and
        room:askForSkillInvoke(player, sk__yanjiao.name, nil, "#sk__yanjiao-damage::" .. target.id .. ":" .. n) then
      room:doIndicate(player.id, { target.id })
      room:damage({
        from = player,
        to = target,
        damage = n,
        skillName = sk__yanjiao.name,
      })
    end
  end,
})

return sk__yanjiao
